<?php

class Home extends CI_Controller {

    var $_public_view;

    function __construct() {
        parent::__construct();
        $this->load->model('model_perjalanan', 'mtrip');
        $this->load->helper('url');
        $this->load->library('session');
        $this->config->load('ojexcom');
        $this->_public_view = $this->config->item('public_view');
    }

    function index() {
        // $this->session->set_userdata('isLogin',true);
        // $this->session->set_userdata('username','bigzaman');
        // $this->session->set_userdata('user_id','19');
        $this->session->set_userdata('page','home');
        $isLogin = $this->session->userdata('isLogin');
        $user = ''; $id='';
        if ($isLogin) {
            $user = $this->session->userdata('username');
            $id = $this->session->userdata('user_id');
        }

        $data = array(
            'output' => $this->get(),
            'user'=> $user, 'id' => $id,
            'isLogin' => $isLogin,
            'page_title' => 'O-JEX',
            'page_content' => 'home');
        $data1 = array('page_content'=>'cari');
       $this->load->view($this->_public_view, $data);
       //$this->load->view('cari');
     }

	function countTimeline() {
        //cek session
        $isLogin = $this->session->userdata('isLogin');
        $user = ''; $id='';
        if ($isLogin) {
            $user = $this->session->userdata('username');
            $id = $this->session->userdata('user_id');
        }

		$p = 0;
		$u = 0;
        if($this->input->post('radius')){
            $myid = $id;
            $radius = $this->input->post('radius');
            $mylat = 0;
            $mylong = 0;
            $query_coor = $this->db->query("SELECT latitude,longitude FROM coordinate WHERE idUser=".$myid);
            $row_coor = $query_coor->row(); 
            if($query_coor ->num_rows() > 0){
                $mylat = $row_coor->latitude;
                $mylong = $row_coor->longitude;
            }
        
            $query = $this->db->query("SELECT t.*, u.`name`,  u.`avatar`, u.lastUpdatedAvatar, u.isPremium, u.gender, c.latitude, c.longitude FROM trip t JOIN `user` u ON u.id=t.userId JOIN coordinate c ON c.idUser=u.id WHERE t.is_available=1 AND departTime> '".date("Y-m-d H:i:s")."' ORDER BY departTime DESC");
            if($query->num_rows() < 1 || $mylat == 0|| $mylong == 0){
                $data_output['timeline'] = "";
            }else{
                $row = $query->row(); 
                $count = 0;
                foreach($query->result() as $row){
                    $distance = $this->distance($mylat,$mylong,$row->latitude,$row->longitude);
                    if( $distance <= $radius){
                        $data_output["timeline"][$count]["id"] = $row->id;
                        $data_output["timeline"][$count]["userId"] = $row->userId;
                        $data_output["timeline"][$count]["name"] = $row->name;
                        $data_output["timeline"][$count]["avatar"] = $row->avatar;
                        $data_output["timeline"][$count]["lastUpdatedAvatar"] = $row->lastUpdatedAvatar;
                        $data_output["timeline"][$count]["gender"] = $row->gender;
                        $data_output["timeline"][$count]["isPremium"] = $row->isPremium;
                        $data_output["timeline"][$count]["status"] = $row->status;
                        $data_output["timeline"][$count]["source_label"] = $row->source_label;
                        $data_output["timeline"][$count]["source_latitude"] = $row->source_latitude;
                        $data_output["timeline"][$count]["source_longitude"] = $row->source_longitude;
                        $data_output["timeline"][$count]["destination_label"] = $row->destination_label;
                        $data_output["timeline"][$count]["destination_latitude"] = $row->destination_latitude;
                        $data_output["timeline"][$count]["destination_longitude"] = $row->destination_longitude;
                        $data_output["timeline"][$count]["via"] = $row->via;
                        $data_output["timeline"][$count]["priceInRupiah"] = $row->priceInRupiah;
                        $data_output["timeline"][$count]["priceInPoint"] = $row->priceInPoint;
                        $data_output["timeline"][$count]["departTime"] = date('d-m-Y H:i:s',strtotime($row->departTime));
                        $data_output["timeline"][$count]["notes"] = $row->notes;
                        $count++;
                        if ($row->status=='U') {
							$u++;
						} else if ($row->status=='P') {
							$p++;
						}
                    }
                }   
                if($count == 0)             $data_output['timeline'] = "";
            }
        }else{
            $query = $this->db->query("SELECT t.*, u.`name`,  u.`avatar`, u.lastUpdatedAvatar, u.isPremium, u.gender, c.latitude, c.longitude FROM trip t JOIN `user` u ON u.id=t.userId JOIN coordinate c ON c.idUser=u.id WHERE t.is_available=1 AND departTime> '".date("Y-m-d H:i:s")."' ORDER BY departTime DESC LIMIT 20");
            if($query->num_rows() < 1){
                $data_output['timeline'] = "";
            }else{
                $row = $query->row(); 
                $count = 0;
                foreach($query->result() as $row){
                    $data_output["timeline"][$count]["id"] = $row->id;
                    $data_output["timeline"][$count]["userId"] = $row->userId;
                    $data_output["timeline"][$count]["name"] = $row->name;
                    $data_output["timeline"][$count]["avatar"] = $row->avatar;
                    $data_output["timeline"][$count]["lastUpdatedAvatar"] = $row->lastUpdatedAvatar;
                    $data_output["timeline"][$count]["gender"] = $row->gender;
                    $data_output["timeline"][$count]["isPremium"] = $row->isPremium;
                    $data_output["timeline"][$count]["status"] = $row->status;
                    $data_output["timeline"][$count]["source_label"] = $row->source_label;
                    $data_output["timeline"][$count]["source_latitude"] = $row->source_latitude;
                    $data_output["timeline"][$count]["source_longitude"] = $row->source_longitude;
                    $data_output["timeline"][$count]["destination_label"] = $row->destination_label;
                    $data_output["timeline"][$count]["destination_latitude"] = $row->destination_latitude;
                    $data_output["timeline"][$count]["destination_longitude"] = $row->destination_longitude;
                    $data_output["timeline"][$count]["via"] = $row->via;
                    $data_output["timeline"][$count]["priceInRupiah"] = $row->priceInRupiah;
                    $data_output["timeline"][$count]["priceInPoint"] = $row->priceInPoint;
                    $data_output["timeline"][$count]["departTime"] = date('d-m-Y H:i:s',strtotime($row->departTime));
                    $data_output["timeline"][$count]["notes"] = $row->notes;
                    $count++;
					if ($row->status=='U') {
						$u++;
					} else if ($row->status=='P') {
						$p++;
					}
                }   
                if($count == 0)             $data_output['timeline'] = "";
            }
        }
        $ret = array('U' => $u, 'P' => $p);
        echo json_encode($ret);
        return $ret;
	}

     function get(){
        //cek session
        $isLogin = $this->session->userdata('isLogin');
        $user = ''; $id='';
        if ($isLogin) {
            $user = $this->session->userdata('username');
            $id = $this->session->userdata('user_id');
        }

        if($this->input->post('radius')){
            $myid = $id;
            $radius = $this->input->post('radius');
            $mylat = 0;
            $mylong = 0;
            $query_coor = $this->db->query("SELECT latitude,longitude FROM coordinate WHERE idUser=".$myid);
            $row_coor = $query_coor->row(); 
            if($query_coor ->num_rows() > 0){
                $mylat = $row_coor->latitude;
                $mylong = $row_coor->longitude;
            }
        
            $query = $this->db->query("SELECT t.*, u.`name`,  u.`avatar`, u.lastUpdatedAvatar, u.isPremium, u.gender, c.latitude, c.longitude FROM trip t JOIN `user` u ON u.id=t.userId JOIN coordinate c ON c.idUser=u.id WHERE  t.is_available=1 AND departTime> '".date("Y-m-d H:i:s")."' ORDER BY departTime DESC");
            if($query->num_rows() < 1 || $mylat == 0|| $mylong == 0){
                $data_output['timeline'] = "";
            }else{
                $row = $query->row(); 
                $count = 0;
                foreach($query->result() as $row){
                    $distance = $this->distance($mylat,$mylong,$row->latitude,$row->longitude);
                    if( $distance <= $radius){
                        $data_output["timeline"][$count]["id"] = $row->id;
                        $data_output["timeline"][$count]["userId"] = $row->userId;
                        $data_output["timeline"][$count]["name"] = $row->name;
                        $data_output["timeline"][$count]["avatar"] = $row->avatar;
                        $data_output["timeline"][$count]["lastUpdatedAvatar"] = $row->lastUpdatedAvatar;
                        $data_output["timeline"][$count]["gender"] = $row->gender;
                        $data_output["timeline"][$count]["isPremium"] = $row->isPremium;
                        $data_output["timeline"][$count]["status"] = $row->status;
                        $data_output["timeline"][$count]["source_label"] = $row->source_label;
                        $data_output["timeline"][$count]["source_latitude"] = $row->source_latitude;
                        $data_output["timeline"][$count]["source_longitude"] = $row->source_longitude;
                        $data_output["timeline"][$count]["destination_label"] = $row->destination_label;
                        $data_output["timeline"][$count]["destination_latitude"] = $row->destination_latitude;
                        $data_output["timeline"][$count]["destination_longitude"] = $row->destination_longitude;
                        $data_output["timeline"][$count]["via"] = $row->via;
                        $data_output["timeline"][$count]["priceInRupiah"] = $row->priceInRupiah;
                        $data_output["timeline"][$count]["priceInPoint"] = $row->priceInPoint;
                        $data_output["timeline"][$count]["departTime"] = date('d-m-Y H:i:s',strtotime($row->departTime));
                        $data_output["timeline"][$count]["notes"] = $row->notes;
                        $count++;
                    }
                }   
                if($count == 0)             $data_output['timeline'] = "";
            }
        }else{
            $query = $this->db->query("SELECT t.*, u.`name`,  u.`avatar`, u.lastUpdatedAvatar, u.isPremium, u.gender, c.latitude, c.longitude FROM trip t JOIN `user` u ON u.id=t.userId JOIN coordinate c ON c.idUser=u.id WHERE  t.is_available=1 AND departTime> '".date("Y-m-d H:i:s")."' ORDER BY departTime DESC LIMIT 20");
            if($query->num_rows() < 1){
                $data_output['timeline'] = "";
            }else{
                $row = $query->row(); 
                $count = 0;
                foreach($query->result() as $row){
                    $data_output["timeline"][$count]["id"] = $row->id;
                    $data_output["timeline"][$count]["userId"] = $row->userId;
                    $data_output["timeline"][$count]["name"] = $row->name;
                    $data_output["timeline"][$count]["avatar"] = $row->avatar;
                    $data_output["timeline"][$count]["lastUpdatedAvatar"] = $row->lastUpdatedAvatar;
                    $data_output["timeline"][$count]["gender"] = $row->gender;
                    $data_output["timeline"][$count]["isPremium"] = $row->isPremium;
                    $data_output["timeline"][$count]["status"] = $row->status;
                    $data_output["timeline"][$count]["source_label"] = $row->source_label;
                    $data_output["timeline"][$count]["source_latitude"] = $row->source_latitude;
                    $data_output["timeline"][$count]["source_longitude"] = $row->source_longitude;
                    $data_output["timeline"][$count]["destination_label"] = $row->destination_label;
                    $data_output["timeline"][$count]["destination_latitude"] = $row->destination_latitude;
                    $data_output["timeline"][$count]["destination_longitude"] = $row->destination_longitude;
                    $data_output["timeline"][$count]["via"] = $row->via;
                    $data_output["timeline"][$count]["priceInRupiah"] = $row->priceInRupiah;
                    $data_output["timeline"][$count]["priceInPoint"] = $row->priceInPoint;
                    $data_output["timeline"][$count]["departTime"] = date('d-m-Y H:i:s',strtotime($row->departTime));
                    $data_output["timeline"][$count]["notes"] = $row->notes;
                    $count++;
                }   
                if($count == 0)             $data_output['timeline'] = "";
            }
        }
        return $data_output;
    }

    function get_timeline($arg = ''){
        //cek session
        $isLogin = $this->session->userdata('isLogin');
        $user = ''; $id='';
        if ($isLogin) {
            $user = $this->session->userdata('username');
            $id = $this->session->userdata('user_id');
        }

        if($this->input->post('radius')){
            $myid = $id;
            $radius = $this->input->post('radius');
            $mylat = 0;
            $mylong = 0;
            $query_coor = $this->db->query("SELECT latitude,longitude FROM coordinate WHERE idUser=".$myid);
            $row_coor = $query_coor->row(); 
            if($query_coor ->num_rows() > 0){
                $mylat = $row_coor->latitude;
                $mylong = $row_coor->longitude;
            }
        
            $query = $this->db->query("SELECT t.*, u.`name`,  u.`avatar`, u.lastUpdatedAvatar, u.isPremium, u.gender, c.latitude, c.longitude FROM trip t JOIN `user` u ON u.id=t.userId JOIN coordinate c ON c.idUser=u.id WHERE t.status='" .$arg. "' AND t.is_available=1 AND departTime> '".date("Y-m-d H:i:s")."' ORDER BY departTime DESC");
            if($query->num_rows() < 1 || $mylat == 0|| $mylong == 0){
                $data_output['timeline'] = "";
            }else{
                $row = $query->row(); 
                $count = 0;
                foreach($query->result() as $row){
                    $distance = $this->distance($mylat,$mylong,$row->latitude,$row->longitude);
                    if( $distance <= $radius){
                        $data_output["timeline"][$count]["id"] = $row->id;
                        $data_output["timeline"][$count]["userId"] = $row->userId;
                        $data_output["timeline"][$count]["name"] = $row->name;
                        $data_output["timeline"][$count]["avatar"] = $row->avatar;
                        $data_output["timeline"][$count]["lastUpdatedAvatar"] = $row->lastUpdatedAvatar;
                        $data_output["timeline"][$count]["gender"] = $row->gender;
                        $data_output["timeline"][$count]["isPremium"] = $row->isPremium;
                        $data_output["timeline"][$count]["status"] = $row->status;
                        $data_output["timeline"][$count]["source_label"] = $row->source_label;
                        $data_output["timeline"][$count]["source_latitude"] = $row->source_latitude;
                        $data_output["timeline"][$count]["source_longitude"] = $row->source_longitude;
                        $data_output["timeline"][$count]["destination_label"] = $row->destination_label;
                        $data_output["timeline"][$count]["destination_latitude"] = $row->destination_latitude;
                        $data_output["timeline"][$count]["destination_longitude"] = $row->destination_longitude;
                        $data_output["timeline"][$count]["via"] = $row->via;
                        $data_output["timeline"][$count]["priceInRupiah"] = $row->priceInRupiah;
                        $data_output["timeline"][$count]["priceInPoint"] = $row->priceInPoint;
                        $data_output["timeline"][$count]["departTime"] = date('d-m-Y H:i:s',strtotime($row->departTime));
                        $data_output["timeline"][$count]["notes"] = $row->notes;
                        $count++;
                    }
                }   
                if($count == 0)             $data_output['timeline'] = "";
            }
        }else{
            $query = $this->db->query("SELECT t.*, u.`name`,  u.`avatar`, u.lastUpdatedAvatar, u.isPremium, u.gender, c.latitude, c.longitude FROM trip t JOIN `user` u ON u.id=t.userId JOIN coordinate c ON c.idUser=u.id WHERE t.status='" .$arg. "' AND t.is_available=1 AND departTime> '".date("Y-m-d H:i:s")."' ORDER BY departTime DESC LIMIT 20");
            if($query->num_rows() < 1){
                $data_output['timeline'] = "";
            }else{
                $row = $query->row(); 
                $count = 0;
                foreach($query->result() as $row){
                    $data_output["timeline"][$count]["id"] = $row->id;
                    $data_output["timeline"][$count]["userId"] = $row->userId;
                    $data_output["timeline"][$count]["name"] = $row->name;
                    $data_output["timeline"][$count]["avatar"] = $row->avatar;
                    $data_output["timeline"][$count]["lastUpdatedAvatar"] = $row->lastUpdatedAvatar;
                    $data_output["timeline"][$count]["gender"] = $row->gender;
                    $data_output["timeline"][$count]["isPremium"] = $row->isPremium;
                    $data_output["timeline"][$count]["status"] = $row->status;
                    $data_output["timeline"][$count]["source_label"] = $row->source_label;
                    $data_output["timeline"][$count]["source_latitude"] = $row->source_latitude;
                    $data_output["timeline"][$count]["source_longitude"] = $row->source_longitude;
                    $data_output["timeline"][$count]["destination_label"] = $row->destination_label;
                    $data_output["timeline"][$count]["destination_latitude"] = $row->destination_latitude;
                    $data_output["timeline"][$count]["destination_longitude"] = $row->destination_longitude;
                    $data_output["timeline"][$count]["via"] = $row->via;
                    $data_output["timeline"][$count]["priceInRupiah"] = $row->priceInRupiah;
                    $data_output["timeline"][$count]["priceInPoint"] = $row->priceInPoint;
                    $data_output["timeline"][$count]["departTime"] = date('d-m-Y H:i:s',strtotime($row->departTime));
                    $data_output["timeline"][$count]["notes"] = $row->notes;
                    $count++;
                }   
                if($count == 0)             $data_output['timeline'] = "";
            }
        }
        //return $data_output;
        header('Content-Type: application/json');
        $output = json_encode($data_output);
        echo($output);
        return $output;
    }

}

?>
